Apache Commons Collections লাইব্রেরি Closure ইন্টারফেস প্রদান করে, যা একটি ফাংশনাল ইন্টারফেস হিসেবে কাজ করে, এবং এটি Java কোডে ডাটা স্ট্রাকচারগুলিতে অপারেশন প্রয়োগ করার জন্য ব্যবহৃত হয়। Closure ইন্টারফেস মূলত একটি ফাংশনাল স্টাইল ডেভেলপমেন্ট প্যাটার্ন অনুসরণ করে এবং এটি একটি একক execute()
পদ্ধতির মাধ্যমে কার্যক্রম সম্পাদন করে।
এই লেখায়, আমরা Closure ইন্টারফেস কিভাবে ব্যবহার করে বিভিন্ন Collection বা Iterator তে ফাংশন প্রয়োগ করা যায়, তা দেখব।
Closure ইন্টারফেসের উদ্দেশ্য হল একটি কার্যপ্রণালী (functionality) এক্সিকিউট (execute) করা যেটি কোনো ইনপুট (যেমন, একটি Object) নেয় এবং এর উপর কোনো অপারেশন (যেমন print, transform, update) প্রয়োগ করে। এটি বিশেষভাবে ব্যবহার হয় যখন আপনি একটি Iterator বা Collection এর প্রতিটি উপাদানের উপর নির্দিষ্ট একটি ফাংশন প্রয়োগ করতে চান।
Closure ইন্টারফেসে একমাত্র পদ্ধতি execute(T input)
থাকে, যেখানে T
হল ইনপুট প্যারামিটার (যে কোন অবজেক্ট)।
Apache Commons Collections এ Closure ইন্টারফেসের ব্যবহার মূলত ফাংশনালিটি প্রয়োগ করার জন্য হয়। নিচে এর ব্যবহার দেখানো হয়েছে।
import org.apache.commons.collections4.functors.Closure;
import org.apache.commons.collections4.functors.LambdaClosure;
import java.util.ArrayList;
import java.util.List;
public class ClosureExample {
public static void main(String[] args) {
// List of strings
List<String> fruits = new ArrayList<>();
fruits.add("Apple");
fruits.add("Banana");
fruits.add("Cherry");
// Closure implementation using LambdaClosure
Closure<String> printClosure = new LambdaClosure<>(fruit -> System.out.println(fruit));
// Applying Closure on each item in the list
for (String fruit : fruits) {
printClosure.execute(fruit); // Execute closure for each fruit
}
}
}
এখানে:
Apple
Banana
Cherry
এখানে, প্রতিটি ফলের নাম প্রিন্ট করার জন্য Closure ব্যবহার করা হয়েছে।
আপনি Collection বা Iterator এর উপর ফাংশন প্রয়োগ করতে পারেন, যেমন প্রতিটি উপাদানে কোন পরিবর্তন বা অপারেশন চালানো।
import org.apache.commons.collections4.functors.Closure;
import org.apache.commons.collections4.functors.LambdaClosure;
import java.util.ArrayList;
import java.util.List;
public class ClosureListExample {
public static void main(String[] args) {
// List of numbers
List<Integer> numbers = new ArrayList<>();
numbers.add(1);
numbers.add(2);
numbers.add(3);
// Closure implementation to double each number
Closure<Integer> doubleClosure = new LambdaClosure<>(number -> System.out.println(number * 2));
// Applying Closure on each item in the list
for (Integer number : numbers) {
doubleClosure.execute(number); // Execute closure to double the number
}
}
}
এখানে:
2
4
6
এখানে, প্রতিটি সংখ্যার উপর গুণ ২ করে প্রিন্ট করা হয়েছে।
IteratorUtils এর সাথে Closure ব্যবহার করলে আপনি Iterator এর উপাদানগুলির উপর কার্যক্রম প্রয়োগ করতে পারেন।
import org.apache.commons.collections4.functors.Closure;
import org.apache.commons.collections4.IteratorUtils;
import org.apache.commons.collections4.functors.LambdaClosure;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
public class IteratorClosureExample {
public static void main(String[] args) {
// List of numbers
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
// Closure implementation to print each number
Closure<Integer> printClosure = new LambdaClosure<>(number -> System.out.println(number));
// Iterator from the list
Iterator<Integer> iterator = numbers.iterator();
// Apply closure using IteratorUtils
IteratorUtils.forEach(iterator, printClosure);
}
}
এখানে:
1
2
3
4
5
এখানে প্রতিটি উপাদান printClosure এর মাধ্যমে প্রিন্ট করা হয়েছে।
Closure ব্যবহার করে আপনি Collection বা Iterator এর প্রতিটি উপাদানের উপর সহজেই ফাংশন প্রয়োগ করতে পারেন, যা খুবই সুবিধাজনক যখন আপনি একটি সুনির্দিষ্ট কাজের জন্য একাধিক উপাদানে একই অপারেশন প্রয়োগ করতে চান।
import org.apache.commons.collections4.functors.Closure;
import org.apache.commons.collections4.functors.LambdaClosure;
import java.util.Arrays;
import java.util.Collection;
public class CollectionClosureExample {
public static void main(String[] args) {
// A collection of numbers
Collection<Integer> numbers = Arrays.asList(10, 20, 30, 40);
// Closure to increase each number by 5
Closure<Integer> increaseByFiveClosure = new LambdaClosure<>(number -> System.out.println(number + 5));
// Apply the closure to each element in the collection
numbers.forEach(number -> increaseByFiveClosure.execute(number));
}
}
এখানে:
15
25
35
45
Closure ইন্টারফেস ব্যবহার করে আপনি Collection বা Iterator এর প্রতিটি উপাদানের উপর নির্দিষ্ট ফাংশন প্রয়োগ করতে পারেন। Apache Commons Collections এর LambdaClosure সহ Closure ইন্টারফেস ব্যবহার করে আপনি সহজে ফিল্টারিং, ট্রান্সফর্মেশন, বা অন্যান্য কার্যক্রম সম্পাদন করতে পারেন। IteratorUtils এর মাধ্যমে আপনি Iterator এর উপাদানগুলির উপর কার্যকরভাবে Closure প্রয়োগ করতে পারেন, যা ডেভেলপারদের কোড লেখার প্রক্রিয়া অনেক সহজ এবং আরও কার্যকরী করে তোলে।
common.read_more